package com.tsingteng.cosfun.ui.found;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.OpusBean;
import com.tsingteng.cosfun.common.Navigate;
import com.tsingteng.cosfun.helper.ActivityManagers;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.utils.ShareUtils;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;



public class FoundVideoActivity extends AppCompatActivity implements ShareUtils.ShareListener,View.OnClickListener{

    OpusBean opusBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_found_video);
        ActivityManagers.getInstance().addFoundActivity(this);
        opusBean= VideoHelper.getIntence().getmOpusBean();
        TextView gofirst_tv=(TextView) findViewById(R.id.gofirst_tv);
        TextView back_tv=(TextView) findViewById(R.id.back_tv);
        TextView share_weixin=(TextView)findViewById(R.id.share_weixin);
        TextView share_pengyouquan=(TextView)findViewById(R.id.share_pengyouquan);
        TextView share_qq=(TextView)findViewById(R.id.share_qq);
        TextView share_kongjian=(TextView)findViewById(R.id.share_kongjian);
        TextView share_weibo=(TextView)findViewById(R.id.share_weibo);
        gofirst_tv.setOnClickListener(this);
        back_tv.setOnClickListener(this);
        share_weixin.setOnClickListener(this);
        share_pengyouquan.setOnClickListener(this);
        share_qq.setOnClickListener(this);
        share_kongjian.setOnClickListener(this);
        share_weibo.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.gofirst_tv:
                int alwaysFinish = Settings.Global.getInt(getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0);
                if (1 ==alwaysFinish) {
                    Navigate.gotoMainActivity(this);
                }else {
                    VideoHelper.getIntence().setActivityId(-1);
                    //删除拍摄的activity
                    ActivityManagers.getInstance().delShootActivity();
                    ActivityManagers.getInstance().finishToMainActivity();
                    //发布成功,播放发布视屏的回调
//                    BroadcastManager.getInstance(this).sendBroadcast(Constant.JOIN);
                    ActivityManagers.getInstance().deleteFoundActivity();
                    finish();
                }
                break;
            case R.id.back_tv:
                int alwaysFinishs = Settings.Global.getInt(getContentResolver(), Settings.Global.ALWAYS_FINISH_ACTIVITIES, 0);
                if (1 == alwaysFinishs){
                    Navigate.gotoMainActivity(this);
                }else {
                    finish();
                    ActivityManagers.getInstance().delShootActivity();
                    ActivityManagers.getInstance().finishPlay();
                    ActivityManagers.getInstance().finishToFoundActivity();
                    ActivityManagers.getInstance().deleteFoundActivity();
                    if (!TextUtils.isEmpty(VideoHelper.getIntence().getDirect())) {
                        ActivityManagers.getInstance().deleteFoundActivity();
                        //删除拍摄的activity
                        ActivityManagers.getInstance().delShootActivity();
                        ActivityManagers.getInstance().finishToFoundActivity();
                        VideoHelper.getIntence().setDirection("");
                    }
                }

                break;
            case R.id.share_weixin:
                if (null == opusBean){
                    return;
                }
                ShareUtils.getIntance().foundShareVideo(this, SHARE_MEDIA.WEIXIN, String.valueOf(opusBean.getId()), opusBean.getVideoUrl(), opusBean.getVideoCverUrl(), opusBean.getProfileName() + ShareUtils.shareTitle, (TextUtils.isEmpty(opusBean.getVideoDesp()) ? ShareUtils.shareDes : opusBean.getVideoDesp()), this);
                break;
            case R.id.share_pengyouquan:
                if (null == opusBean){
                    return;
                }
                ShareUtils.getIntance().foundShareVideo(this, SHARE_MEDIA.WEIXIN_CIRCLE, String.valueOf(opusBean.getId()), opusBean.getVideoUrl(), opusBean.getVideoCverUrl(), opusBean.getProfileName() + ShareUtils.shareTitle, (TextUtils.isEmpty(opusBean.getVideoDesp()) ? ShareUtils.shareDes : opusBean.getVideoDesp()), this);
                break;
            case R.id.share_qq:
                if (null == opusBean){
                    return;
                }
                ShareUtils.getIntance().foundShareVideo(this, SHARE_MEDIA.QQ, String.valueOf(opusBean.getId()), opusBean.getVideoUrl(), opusBean.getVideoCverUrl(), opusBean.getProfileName() + ShareUtils.shareTitle, (TextUtils.isEmpty(opusBean.getVideoDesp()) ? ShareUtils.shareDes : opusBean.getVideoDesp()), this);
                break;
            case R.id.share_kongjian:
                if (null == opusBean){
                    return;
                }
                ShareUtils.getIntance().foundShareVideo(this, SHARE_MEDIA.QZONE, String.valueOf(opusBean.getId()), opusBean.getVideoUrl(), opusBean.getVideoCverUrl(), opusBean.getProfileName() + ShareUtils.shareTitle, (TextUtils.isEmpty(opusBean.getVideoDesp()) ? ShareUtils.shareDes : opusBean.getVideoDesp()), this);
                break;
            case R.id.share_weibo:
                if (null == opusBean){
                    return;
                }
                ShareUtils.getIntance().foundShareVideo(this, SHARE_MEDIA.SINA, String.valueOf(opusBean.getId()), opusBean.getVideoUrl(), opusBean.getVideoCverUrl(), opusBean.getProfileName() + ShareUtils.shareTitle, (TextUtils.isEmpty(opusBean.getVideoDesp()) ? ShareUtils.shareDes : opusBean.getVideoDesp()), this);
                break;
        }
    }

    @Override
    public void onShareFinish() {

    }

    @Override
    public void onShareSuccess() {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }
}
